package cc.yaomm.service.basic.controller.business;

import cc.yaomm.common.basic.base.SuperBiz;
import cc.yaomm.common.basic.constant.DefaultConstant;
import cc.yaomm.common.basic.exception.BusinessException;
import cc.yaomm.common.basic.util.ConvertUtils;
import cc.yaomm.service.basic.service.user.bo.LoginResultBO;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.util.HtmlUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Objects;

/**
 * Created by XQL on 2021/6/16.
 */
@Slf4j
public class BaseAdminBiz extends SuperBiz {

    /**
     * <p>@Description : 获取用户登录信息</p>
     * <p>@Author : QiLin.Xing </p>
     * <p>@Date : 2018/8/9 14:48 </p>
     */
    protected LoginResultBO getUserLogin() {
        String loginInfoStr = getRequest().getHeader(DefaultConstant.USER_LOGIN_INFO);
        if (!StringUtils.hasText(loginInfoStr)) {
            return null;
        }
        try {
            loginInfoStr = URLDecoder.decode(loginInfoStr, DefaultConstant.UTF8);
        } catch (UnsupportedEncodingException e) {
            log.error("解析用户token信息异常", e);
            return null;
        }
        //loginInfoStr = HtmlUtils.htmlUnescape(loginInfoStr);
        return JSON.parseObject(loginInfoStr, LoginResultBO.class);
    }

    /**
     * <p>@Description : 获取登录用户ID</p>
     * <p>@Author : QiLin.Xing </p>
     * <p>@Date : 2018/8/9 14:48 </p>
     */
    protected Long getUserId() {
        String requestUid = getRequest().getHeader(DefaultConstant.REQUEST_UID);
        return ConvertUtils.toLong(requestUid, null);
    }

    /**
     * <p>@Description : 获取操作人信息 </p>
     * <p>@Author : QiLin.Xing </p>
     * <p>@Date : 2021/5/28 15:17 </p>
     */
    protected String getUserName() {
        LoginResultBO loginBO = this.getUserLogin();
        if (Objects.nonNull(loginBO)) {
            return loginBO.getUserName();
        }
        throw new BusinessException("获取当前登录用户名异常");
    }

}
